package com.superman.supermarket.client.composite.cart.sku;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.widget.lazy.LazyImage;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.composite.cart.ICheckModule;
import com.superman.supermarket.client.constant.CartSkuOperateConstants;
import com.superman.supermarket.client.event.CartPageEvent;
import com.superman.supermarket.client.event.CartPageEvent.CartPageEventType;
import com.superman.supermarket.client.utils.ActivityJumpUrl;
import com.superman.supermarket.client.widget.button.CheckButton;
import com.superman.supermarket.client.widget.numbereditor.NumCounter;

/**
 * 购物车商品模块
 * 
 * @author superman
 * @version 2018年8月7日上午10:22:11
 */
public class CartSkuModule extends Composite implements IUIModule, ICheckModule {

	interface CartSkuModuleUiBinder extends UiBinder<Widget, CartSkuModule> {
	}

	private static CartSkuModuleUiBinder uiBinder = GWT.create(CartSkuModuleUiBinder.class);
	@UiField
	protected CheckButton checkbtn;
	private Map<String, Object> map;
	@UiField
	NumCounter numinput;
	private String skuId;
	@UiField
	protected LazyImage skuimg;
	@UiField
	protected HTML skunamehtml, skupropnameshtml, salepricehtml, quotahtml;
	private int skuNum, quota, stock;

	private String storeId, spuId, props;
	@UiField
	protected FlowPanel wrapperpanel, counteditpanel, skupanel;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月7日 上午10:22:13
	 */
	public CartSkuModule() {
		initWidget(uiBinder.createAndBindUi(this));

		skupanel.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				event.stopPropagation();
				ActivityJumpUrl.pushToSkuDetailPage(spuId, props);
			}
		}, ClickEvent.getType());
	}

	@Override
	public void check() {
		if (!isCheck()) {
			checkbtn.setCheck(true);
		}
	}

	@UiHandler("checkbtn")
	void check(ClickEvent event) {
		// 点击触发选中或者取消选中商品的事件.有两个地方监听,分别是店铺的监听和activiti层的监听
		CartPageEvent cartPageEvent = new CartPageEvent(this.isCheck() ? CartPageEventType.CHECK_SKU : CartPageEventType.UNCHECK_SKU,
				getData());
		XGinjector.INSTANCE.getEventbusController().fire(cartPageEvent);
	}

	@UiHandler("deletebtn")
	void delete(ClickEvent event) {
		event.stopPropagation();
		CartPageEvent cartPageEvent = new CartPageEvent(CartPageEventType.DELETE, skuId);
		XGinjector.INSTANCE.getEventbusController().fire(cartPageEvent);
	}

	@Override
	public Map<String, Object> getData() {
		Map<String, Object> skuMap = new HashMap<String, Object>();
		skuMap.put("SKU_ID", skuId);
		skuMap.put("SKU_NUM", this.numinput.getValue());
		skuMap.put("STORE_ID", this.storeId);
		return skuMap;
	}

	@Override
	public boolean isCheck() {
		return checkbtn.isCheck();
	}

	@Override
	public void setData(Map<String, Object> map) {
		this.map = map;
		this.skuId = String.valueOf(map.get("SKU_ID"));
		this.spuId = String.valueOf(map.get("SPU_ID"));
		this.props = String.valueOf(map.get("PROPS"));
		this.skuNum = (int) Double.parseDouble(String.valueOf(map.get("NUM")));
		this.quota = (int) Double.parseDouble(String.valueOf(map.get("QUOTA")));
		this.stock = (int) Double.parseDouble(String.valueOf(map.get("STOCK")));
		skuimg.setUrl(String.valueOf(map.get("PIC")));
		skunamehtml.setText(String.valueOf(map.get("NAME")));
		skupropnameshtml.setText(String.valueOf(map.get("PROPS_NAME")));
		salepricehtml.setHTML("¥&nbsp;" + map.get("SALE_PRICE"));

		// 限购的处理
		if (quota == 0)
			quotahtml.setVisible(false);
		else
			quotahtml.setHTML("限购:&nbsp" + quota);

		if (quota == 0)
			numinput.setMax(stock);
		else
			numinput.setMax(stock > quota ? quota : stock);
		numinput.reset(skuNum);
	}

	/**
	 * 设置店铺id
	 * 
	 * @author superman
	 * @version 2018年8月9日 下午3:11:59
	 * @param storeId
	 */
	public void setStoreId(String storeId) {
		this.storeId = storeId;
	}

	@Override
	public void uncheck() {
		if (isCheck()) {
			checkbtn.setCheck(false);
		}
	}

	@UiHandler("checkbtn")
	void update(ValueChangeEvent<Boolean> event) {
		Map<String, Object> skuPriceMap = new HashMap<String, Object>();
		skuPriceMap.put("SKU_ID", skuId);
		skuPriceMap.put("SKU_NUM", this.numinput.getValue());
		skuPriceMap.put("SALE_PRICE", map.get("SALE_PRICE"));
		skuPriceMap.put("TYPE", event.getValue() ? CartSkuOperateConstants.ADD_SKU : CartSkuOperateConstants.SUB_SKU);
		XGinjector.INSTANCE.getEventbusController().fire(new CartPageEvent(CartPageEventType.UPDATE_PRICE, skuPriceMap));
	}

	@UiHandler("numinput")
	void updateSkuNum(final ValueChangeEvent<Map<String, Integer>> event) {
		final int afterValue = event.getValue().get("AFTER");
		Map<String, Object> skuMap = new HashMap<String, Object>();
		skuMap.put("SKU_ID", skuId);
		skuMap.put("SKU_NUM", afterValue);

		List<Map<String, Object>> editSkus = new ArrayList<Map<String, Object>>();
		editSkus.add(skuMap);

		Map<String, Object> postMap = new HashMap<String, Object>();
		postMap.put("EDIT_LIST", editSkus);

		XGinjector.INSTANCE.getPostMan().post("cart/edit", postMap, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {
				// 修改失败,需要重制
				int beforeValue = event.getValue().get("BEFORE");
				numinput.reset(beforeValue);
			}

			@Override
			public void onBusinessOK(String response) {
				XGinjector.INSTANCE.getEventBus().fireEvent(new ToastEvent("修改数量成功"));

				Map<String, Object> skuPriceMap = new HashMap<String, Object>();
				skuPriceMap.put("SKU_ID", skuId);
				skuPriceMap.put("SKU_NUM", afterValue);
				skuPriceMap.put("SALE_PRICE", map.get("SALE_PRICE"));
				skuPriceMap.put("TYPE", CartSkuOperateConstants.UPDATE_NUM);
				XGinjector.INSTANCE.getEventbusController().fire(new CartPageEvent(CartPageEventType.UPDATE_PRICE, skuPriceMap));
			}
		});
	}

}
